import numpy as np
import matplotlib.pyplot as plt

"""
该函数有无穷多个局部最小值，我们选择学习率，但最终也只能得到多个解中的一个
"""

w = np.linspace(-10, 10, 30)
theta = 0.15 * np.pi
e = w * np.cos(theta * w)

current_w = -4
lr = 1
num_epochs = 100
predict_w = []
for i in range(num_epochs):
    predict_w.append(current_w)
    slope = - theta * current_w * np.sin(theta * current_w)
    # SGD 小固定值 * 斜率
    current_w -= lr * slope
    predict_w.append(current_w)

print(predict_w)
predict_w = np.array(predict_w)
plt.plot(w, e, 'r-')
plt.plot(predict_w, predict_w * np.cos(theta * predict_w), 'bo-')
plt.show()
